<template>
  <div>
<el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>商品管理</el-breadcrumb-item>
  <el-breadcrumb-item>商品列表</el-breadcrumb-item>
</el-breadcrumb>


 <el-card>
   <el-row :gutter="20">
     <el-col :span="6">
      <el-input placeholder="请输入内容" v-model="keyword" class="input-with-select">
        <el-button slot="append" icon="el-icon-search"></el-button>
      </el-input>
     </el-col>
     <el-col :span="18">
       <el-button type="primary">添加商品</el-button>
     </el-col>
   </el-row>
   <!-- 表格 -->
   <el-table
    :data="goods"
    border
    style="width: 100%">
    <el-table-column
      type="index"
      label="#">
    </el-table-column>
    <el-table-column
      prop="goods_name"
      label="商品名称"
      width="180">
    </el-table-column>
    <el-table-column
      prop="goods_price"
      label="商品价格"
      width="180">
    </el-table-column>
    <el-table-column
      prop="goods_weight"
      label="商品重量"
      width="180">
    </el-table-column>
    <el-table-column
      prop="add_time"
      label="创建时间"
      width="180">
      <template slot-scope="scope">
        {{scope.row.add_time | formDate}}
      </template>
    </el-table-column>
    <el-table-column
      label="操作">
       <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

<!--  -->
<el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

 </el-card>



 <!-- 添加商品 -->
<el-dialog title="添加商品" :visible.sync="dialogFormVisible">
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="商品名称" prop="goods_name">
      <el-input v-model="ruleForm.goods_name"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="goods_price">
      <el-input type="number" v-model="ruleForm.goods_price"></el-input>
    </el-form-item>
    <el-form-item label="商品数量" prop="goods_number">
      <el-input  type="number" v-model="ruleForm.goods_number"></el-input>
    </el-form-item>
    <el-form-item label="商品重量" prop="goods_weight">
      <el-input  type="number" v-model="ruleForm.goods_weight"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" >
      <el-upload
        class="upload-demo"
        action="https://jsonplaceholder.typicode.com/posts/"
        :on-change="handlePreview"
        :on-remove="handleRemove"
        :file-list="fileList"
        :auto-upload="false"
        list-type="picture">
        <el-button size="small" type="primary">点击上传</el-button>
        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
      </el-upload>
    </el-form-item>
  
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
  </div>
</el-dialog>

<!-- 修改 -->
<el-dialog title="添加商品" :visible.sync="dialogFormVisible1">
  <el-form :model="ruleForm1" :rules="rules1" ref="ruleForm1" label-width="100px" class="demo-ruleForm">
    <el-form-item label="商品名称" prop="goods_name">
      <el-input v-model="ruleForm1.goods_name"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="goods_price">
      <el-input type="number" v-model="ruleForm1.goods_price"></el-input>
    </el-form-item>
    <el-form-item label="商品数量" prop="goods_number">
      <el-input  type="number" v-model="ruleForm1.goods_number"></el-input>
    </el-form-item>
    <el-form-item label="商品重量" prop="goods_weight">
      <el-input  type="number" v-model="ruleForm1.goods_weight"></el-input>
    </el-form-item>
  
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitForm1('ruleForm1')">确 定</el-button>
  </div>
</el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        keyword: "",
        goods:[],
        pagenum:1,
        pagesize:3,
        total:0,
        dialogFormVisible:false,
         ruleForm: {
           goods_name:"",
           goods_price:0,
           goods_number:0,
           goods_weight:0
         },
        rules: {
          goods_name: [
            { required: true, message: '请输入活动名称', trigger: 'blur' }
          ],
          goods_price: [
            { required: true, message: '请输入活动名称', trigger: 'blur' }
          ],
          goods_number: [
            { required: true, message: '请输入活动名称', trigger: 'blur' }
          ],
          goods_weight: [
            { required: true, message: '请输入活动名称', trigger: 'blur' }
          ]
        },
        fileList:[],
        dialogFormVisible1:false,
         ruleForm1: {
           goods_name:"",
           goods_price:0,
           goods_number:0,
           goods_weight:0
         },
        rules1: {
          goods_name: [
            { required: true, message: '请输入活动名称', trigger: 'blur' }
          ],
          goods_price: [
            { required: true, message: '请输入活动名称', trigger: 'blur' }
          ],
          goods_number: [
            { required: true, message: '请输入活动名称', trigger: 'blur' }
          ],
          goods_weight: [
            { required: true, message: '请输入活动名称', trigger: 'blur' }
          ]
        },
      }
    },
    created () {
      this.getdata();
    },
    methods: {
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file,0);
      },
      submitForm1(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            console.log(this.ruleForm1)
            this.$axios.put(`goods/${this.ruleForm1.goods_id}`,this.ruleForm1)
            .then(res=>{
              console.log(res,'xg')
              if(res.meta.status==200){
                this.$message(res.meta.msg)
                this.dialogFormVisible1=false
                this.getdata()
              }else{
                 this.$message(res.meta.msg)
              }
            })

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // alert('submit!');
            console.log(this.ruleForm)

            // 
            this.$axios.post('goods',this.ruleForm)
            .then(res=>{
              console.log(res,"tjsp")
              if(res.meta.status==201){
                this.$message(res.meta.msg)
                this.getdata()
                this.dialogFormVisible=false
              }else{
                this.$message(res.meta.msg)
              }
            })

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pagesize=val
        this.getdata()
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.pagenum=val
        this.getdata()
      },
      getdata(){
        this.$axios.get('goods',{
          params:{
            query:this.keyword,
            pagenum:this.pagenum,
            pagesize:this.pagesize
          }
        })
        .then(res=>{
          console.log(res.data.goods,'sp')
          this.goods=res.data.goods
          this.total=res.data.total
        })
      },
      handleEdit(index, row) {
        console.log(index, row);
        this.dialogFormVisible1=true
        this.ruleForm1=row
      },
      handleDelete(index, row) {
        console.log(index, row);
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          
          this.$axios.delete(`goods/${row.goods_id}`)
          .then(res=>{
            console.log(res,"delete")
            if(res.meta.status==200){
              this.$message(res.meta.msg)
              this.getdata()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      }
    },
  }
</script>

<style scoped>
.el-card{
  margin-top: 15px;
}
</style>